<template>
  <div class="login-container">
    <!-- 左侧大图 -->
    <div class="logo" />
    <el-card class="box">
      <h1>智慧园区-登录</h1>
      <el-form ref="loginForm" label-position="top" :model="loginForm" :rules="loginRules">
        <el-form-item prop="account" label="账号">
          <el-input v-model="loginForm.account" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input v-model="loginForm.password" type="password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="loginForm.isAgree">记住我</el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button style="width:400px" type="primary" @click="onSubmit">登录</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { loginApi } from '@/api/user'
import { setToken } from '@/utils/auth'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        account: 'demo',
        password: 'Hmzs%001',
        isAgree: false
      },
      loginRules: {
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]

      }
    }
  },
  methods: {
    async onSubmit() {
      try {
        await this.$refs.loginForm.validate()
        const { account, password } = this.loginForm
        const res = await loginApi({ account, password })
        setToken(res.data.token)
        this.$router.replace('/')
        // console.log(res.data.token)
      } catch (error) {
        this.$message.error('账号密码输入有误')
      }
    }
  }

}
</script>

<style lang="scss" scoped>
.login-container {
  height: 100vh;
  display: flex;
  align-items: stretch;

  .logo {
    flex: 3;
    background: rgba(154, 54, 22, 0.9) url('../../assets/common/login-bg.svg') no-repeat center / cover;
    border-top-right-radius: 60px;

  }

  .box {
    flex: 2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding-left: 150px;
    h1 {
      font-size: 26px;
      font-weight: 500;
      color: #1e2023;
    }
    ::v-deep .el-input {
      width: 400px;
      height: 44px;

    }
    ::v-deep .el-checkbox {
      color: #606266;
    }

  }
}
</style>
